package com.dgut.service.background.controller;

import com.dgut.service.background.bean.PassConditions;
import com.dgut.service.background.service.ExportExcelService;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;

/**
 * 导出文件
 */
@Controller
@RequestMapping("export")
public class ExportExcelController {
    @Autowired
    private ExportExcelService exportExcelService;

    //导出所有用户excel
    @GetMapping("exportUser")
    public void exportUser(HttpServletResponse response) throws IOException {
        //设置文件编码
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/ynd.ms-excel;charset=UTF-8");
        XSSFWorkbook workbook = exportExcelService.createUserExcel();
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }

    //按条件导出出、进门excel
    @PostMapping("exportPass/{passType}")
    public void exportPass(@PathVariable("passType")String passType, HttpServletResponse response, PassConditions passConditions) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/ynd.ms-excel;charset=UTF-8");
        XSSFWorkbook workbook = exportExcelService.createPassExcel(passType, passConditions);
        OutputStream outputStream = response.getOutputStream();
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        workbook.close();
    }
}
